package com.biz.primus.ms.coupon.dao.ro;

import com.biz.primus.base.redis.annotation.Ro;
import com.biz.primus.base.redis.annotation.RoSortedSet;
import com.biz.primus.base.redis.bean.BaseRedisObject;
import com.biz.primus.model.coupon.enums.CouponGenerateStrategy;
import com.biz.primus.model.coupon.enums.CouponLimitType;
import com.biz.primus.model.coupon.enums.CouponScopeType;
import java.time.LocalDateTime;
import java.util.Objects;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

/**
 * 优惠券类型RO
 *
 * @author david-liu
 */
@Ro(key = "cpn:coupon_type")
@RoSortedSet(key = "list", score = "createTimestamp")
@Getter
@Setter
public class CouponTypeRo extends BaseRedisObject<Long> {
    private static final long serialVersionUID = 8664636970817992876L;

    //限制可以使用优惠券的分类id, 以逗号分割
    private String categories;

    //限制可以使用优惠券的品牌id, 以逗号分割
    private String brands;

    //限制可以使用的商品id, 以逗号分割
    private String includeProducts;

    //限制不可以使用优惠券的商品id, 与includeProducts二选一填写,如果都为空,则不限制商品
    private String excludeProducts;

    //限制可以使用优惠券的商家
    private String vendors;

    //优惠券限制使用类型
    private CouponLimitType couponLimitType;

    //优惠券使用范围类型(平台券/商家券)
    private CouponScopeType couponScopeType;

    // 优惠券生成策略
    private CouponGenerateStrategy couponGenerateStrategy;

    //券类型名称
    private String couponTypeName;

    //优惠券介绍
    private String couponTypeDesc;

    //优惠券面值, 单位(分)
    private Integer faceValue;

    //优惠券门槛, 单位(分)
    private Integer couponLimitAmount;

    //优惠券可用开始时间
    private LocalDateTime usableStartTime;

    //优惠券可用终止时间
    private LocalDateTime usableEndTime;

    //优惠券可领开始时间
    private LocalDateTime receivableStartTime;

    //优惠券可领终止时间
    private LocalDateTime receivableEndTime;

    //优惠券领取开始之后, 几天后优惠券不可用
    private Integer usableCountDownDays;

    //券类型发行总数 不可修改
    private Integer totalQuantity;

    // 优惠券是否启用
    private Boolean enable;

    // 优惠券图片, 如果需要使用图片, 赋值
    private String icon;

    // 优惠券备注信息
    private String remark;

    // 已领取优惠券数量
    private Integer receivedQuantity = 0;

    // 优惠券类型创建人
    private String creator;

    // 优惠券类型最后更新人
    private String lastUpdateUser;

    public Boolean isPreBuildCoupon() {
        return Objects.isNull(this.couponGenerateStrategy) || this.couponGenerateStrategy == CouponGenerateStrategy.PRE_BUILD;
    }

    public Boolean isTimeInReceivableTimeRange(LocalDateTime localDateTime) {
        return Objects.nonNull(this.getReceivableStartTime()) &&
                Objects.nonNull(this.getReceivableEndTime()) &&
                this.getReceivableStartTime().isBefore(localDateTime) &&
                this.getReceivableEndTime().isAfter(localDateTime);
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE);
    }

}
